﻿var mode = 1;
var oneKmEqMile = 0.621371;
var oneMileFeets = 5280;
var oneKmM = 1000;
var oneXy = oneKmM;
function attachCommontHandlres() {
    var btnMiles = document.getElementById("btnMiles");
    btnMiles.addEventListener("click", buttonSwich, false);

    var btnMeters = document.getElementById("btnMeters");
    btnMeters.addEventListener("click", buttonSwich, false);
}
function buttonSwich(eventInfo) {
    var btn = eventInfo.srcElement;
    btn.disabled = true;
    var n = btn.id;

    if (n === "btnMeters") {
        mode = 1;
        var other = document.getElementById("btnMiles");
        other.disabled = false;
        oneXy = oneKmM;
        setMetersTexts();
        updateSpeed();
    } else {
        mode = 2;
        var other = document.getElementById("btnMeters");
        other.disabled = false;
        oneXy = oneMileFeets;
        setMilesTexts();
        updateSpeed();
    }
}
function setMilesTexts() {
    settext("l_speed", "mil/h");
    settext("l_time", "min/mil");
    settext("l_oposit", "km/h");
    var sel = document.getElementById("slct_dist");
    sel.innerHTML = "<option>Miles</option><option>Foots</option>";
}
function setMetersTexts() {
    settext("l_speed", "km/h");
    settext("l_time", "min/km");
    settext("l_oposit", "mil/h");
    var sel = document.getElementById("slct_dist");
    sel.innerHTML = "<option>Kilometers</option><option>Meters</option>";
}
function settext(id, newtext) {
    var obj = document.getElementById(id);
    obj.innerText = newtext;
}
function getFloatValue(id) {
    var obj = document.getElementById(id);
    var v = obj.value;
    return parseFloat(v);
}
function getSpeed(topDistance, topTime) {
    return topDistance / topTime;
}
function getDistance(d) {
    var s = document.getElementById("slct_dist").selectedIndex;
    if (s == 0)
        return d;
    if (s == 1)
        return d / oneXy;
}
function getTopTime(t) {
    var s = document.getElementById("slct_time").selectedIndex;
    if (s == 0)
        return t/3600;
    if (s == 1)
        return t / 60;
    if (s == 2)
        return t;
}
function getTime(t) {
    var s = document.getElementById("slct_time").selectedIndex;
    if (s == 0)
        return t * 3600;
    if (s == 1)
        return t * 60;
    if (s == 2)
        return t;
}
function setFloatValue(id, v) {
    var obj = document.getElementById(id);
    obj.value = v;
}
function getOppositSpeed(s) {
    if (mode == 1) {
        return s * oneKmEqMile;
    } else {
        return s / oneKmEqMile;
    }
}
function getNikeStyleSpeed(s) {
    return 60 / s;
}

function getFromOppositSpeed(s) {
    if (mode == 1) {
        return s / oneKmEqMile;
    } else {
        return s * oneKmEqMile;
    }
}